// pages/pay/pay.ts
import Toast from "@vant/weapp/toast/toast"
import { axios } from "../../utils/request"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    list: [1, 2, 3, 4, 5, 6, 7, 8, 9],
    password: <Array<number>>[],
    order: {} as any,
  },
  //展示支付弹窗
  showPopup() {
    Toast.loading({
      message: "支付中...",
      duration: 1000,
      forbidClick: true,
      loadingType: "spinner",
      onClose: () => {
        this.setData({
          show: true,
        })
      },
    })
  },
  //取消支付关闭弹窗
  async closePopup() {
    this.setData({ show: false })
    let order = this.data.order
    order.status = 5
    await axios("/order", "POST", {
      ...this.data.order,
    })
    wx.reLaunch({ url: "/pages/order/order" })
  },
  async getPassword(e: any) {
    if (this.data.password.length == 5) {
      this.data.password.push(e.currentTarget.dataset.number)
      this.setData({ password: this.data.password, show: false })
      try {
        await axios("/order", "POST", {
          ...this.data.order,
        })
        Toast.success({
          type: "success",
          message: "提交成功",
          duration: 1000,
          onClose: async () => {
            this.setData({ password: [] })
            wx.reLaunch({ url: "/pages/order/order" })
          },
        })
      } catch (error) {
        Toast.success({
          type: "fail",
          message: "提交失败",
          duration: 1000,
          onClose: async () => {
            this.setData({ password: [] })
          },
        })
      }
    } else if (this.data.password.length < 5) {
      this.data.password.push(e.currentTarget.dataset.number)
      this.setData({ password: this.data.password })
    } else {
      return
    }
  },
  removePassword() {
    this.data.password.pop()
    this.setData({ password: this.data.password })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let order = this.data.order as any
    const { start, end } = getApp().globData
    order.start = start.format("YYYY-MM-DD")
    order.end = end.format("YYYY-MM-DD")
    order.price = getApp().globData.roomOrder.price
    order.userName = getApp().globData.userInfo.userName
    order.number = getApp().globData.userInfo.number
    order.tips = getApp().globData.userInfo.tips
    order.roomId = getApp().globData.userInfo.roomId
    const { name, photos } = getApp().globData.roomOrder.info as Room
    order.name = name
    order.cover = photos[0].url
    order.type = 0
    order.userId = wx.getStorageSync("userInfo").id
    this.setData({
      order,
    })
  },
})
